﻿using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Diagnostics;

namespace SGLLv2.UI
{
    class TextBoxWartermarked : TextBox
    {
        //Declare Global Variables
        private readonly TransparentLabel transparentLabel;

        //Properties
        /// <summary>
        /// Text to be displayed on the textbox.
        /// </summary>
        [Browsable(true)]
        public string WatermarkText
        {
            get
            {
                return transparentLabel.Text;
            }
            set
            {
                transparentLabel.Text = value;
            }
        }
        /// <summary>
        /// Fore color of the watermark
        /// </summary>
        [Browsable(true)]
        public Color WatermarkColor
        {
            get
            {
                return transparentLabel.ForeColor;
            }
            set
            {
                transparentLabel.ForeColor = value;
            }
        }
        /// <summary>
        /// Font type of the watermark
        /// </summary>
        [Browsable(true)]
        public Font WatermarkFont
        {
            get
            {
                return transparentLabel.Font;
            }
            set
            {
                transparentLabel.Font = value;
            }
        }
        /// <summary>
        /// Initialize a new instance of Watermark
        /// </summary>
        public TextBoxWartermarked()
        {
            transparentLabel = new TransparentLabel();
            transparentLabel.Click += label_Click;
            transparentLabel.TextAlign = ContentAlignment.MiddleLeft;
            transparentLabel.Dock = DockStyle.Fill;
            Controls.Add(transparentLabel);
            Controls.SetChildIndex(transparentLabel, 1);
        }
        //Event Handlers
        [DebuggerNonUserCodeAttribute]
        void label_Click(object sender, EventArgs e)
        {
            OnGotFocus(e);
            Focus();
        }
        [DebuggerNonUserCodeAttribute]
        protected override void OnTextChanged(EventArgs e)
        {
            transparentLabel.Visible = string.IsNullOrWhiteSpace(base.Text);
            base.OnTextChanged(e);
        }
        [DebuggerNonUserCodeAttribute]
        protected override void OnClick(EventArgs e)
        {
            transparentLabel.Visible = false;
            base.OnClick(e);
        }
        [DebuggerNonUserCodeAttribute]
        protected override void OnGotFocus(EventArgs e)
        {
            transparentLabel.Visible = false;
            SelectAll();
            base.OnGotFocus(e);
        }
        [DebuggerNonUserCodeAttribute]
        protected override void OnLostFocus(EventArgs e)
        {
            transparentLabel.Visible = string.IsNullOrWhiteSpace(base.Text);
            base.OnLostFocus(e);
        }
    }
}